//
//  MLXMLXOrientationTool.m
//  MLX
//
//  Created by SY on 2018/10/5.
//  Copyright © 2018年 SY. All rights reserved.
//

#import "MLXOrientationTool.h"

@interface MLXOrientationTool ()
/**默认旋转方向*/
@property (nonatomic, assign) UIInterfaceOrientationMask orientationMask;
@property (nonatomic, assign) NSInteger allowRotate;
@property(nonatomic,copy) void(^changeOrientationBlock)(BOOL);
@end

/**检测最大次数，自增*/
static int32_t _t_32_accelerometer = 0;
/**the max number which cmmotionManager can update*/
static NSInteger const kMaxAccelerometerUpdatesNumber = 3;

@implementation MLXOrientationTool
#pragma mark - singltonInstance
+(instancetype)sharedInstance
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
- (void)handleDeviceOrientationDidChange:(UIInterfaceOrientation)interfaceOrientation{
    //1.获取 当前设备 实例
    UIDevice *device = [UIDevice currentDevice] ;
    [self orientationLogicWithOrientation:device.orientation];
}
- (void)getChangeOrientation:(void(^)(BOOL orientationMask))changeOrientationBlock{
    self.changeOrientationBlock = changeOrientationBlock;
}
#pragma mark - public
+ (void)setDefaultOrientationMask:(UIInterfaceOrientationMask)orientationMask {
    [MLXOrientationTool sharedInstance].orientationMask = orientationMask;
}

+ (UIInterfaceOrientationMask)getDefaultOrientationMask {
    return [MLXOrientationTool sharedInstance].orientationMask;
}

+ (NSUInteger)getOrientationType {
    return [MLXOrientationTool sharedInstance].allowRotate;
}


/**
 typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
 UIDeviceOrientationUnknown,
 UIDeviceOrientationPortrait,            // Home按钮在下
 UIDeviceOrientationPortraitUpsideDown,  // Home按钮在上
 UIDeviceOrientationLandscapeLeft,       // Home按钮右
 UIDeviceOrientationLandscapeRight,      // Home按钮左
 UIDeviceOrientationFaceUp,              // 手机平躺，屏幕朝上
 UIDeviceOrientationFaceDown             //手机平躺，屏幕朝下
 } __TVOS_PROHIBITED;
 
 typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
 UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
 UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
 UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
 UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
 UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
 } __TVOS_PROHIBITED;
 
 */
+ (void)orientationMaskAll {
    //设备旋转通知
    [[NSNotificationCenter defaultCenter] addObserver:[MLXOrientationTool sharedInstance]
                                             selector:@selector(handleDeviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [MLXOrientationTool sharedInstance].allowRotate = YBOrientationMaskAll;
}

- (void)orientationLogicWithOrientation:(UIDeviceOrientation)orientation {
    
    //UIInterfaceOrientation 是你程序界面的当前旋转方向 这个可以设置
    UIInterfaceOrientation interfaceOriention = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(interfaceOriention)) {
    }else if (UIInterfaceOrientationIsPortrait(interfaceOriention)) {
    }
    
    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {//水平
        //强制转为横屏
        !self.changeOrientationBlock ?: self.changeOrientationBlock(NO);
        [[MLXOrientationTool sharedInstance] invocationMethodWithOrientation:(UIInterfaceOrientation)orientation];
    }else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {//竖直
        //强制转为竖屏
        !self.changeOrientationBlock ?: self.changeOrientationBlock(YES);
        [[MLXOrientationTool sharedInstance] invocationMethodWithOrientation:(UIInterfaceOrientation)orientation];
    }
}
+ (void)orientationMaskPortrait {
    [MLXOrientationTool sharedInstance].changeOrientationBlock = nil;
    [MLXOrientationTool sharedInstance].allowRotate = YBOrientationMaskPortrait;
    [[MLXOrientationTool sharedInstance] invocationMethodWithOrientation:UIDeviceOrientationPortrait];
    //设备旋转通知
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
- (void) invocationMethodWithOrientation:(int)orientation {
    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: orientation] forKey:@"orientation"];
}
+ (CGFloat)getOrientationMaskWidth{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
+ (CGFloat)getOrientationMaskHeight{
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        height = [UIScreen mainScreen].bounds.size.width;
    }
    return height;
}
@end
